package com.nimt.hqgl_idea.service;

import com.nimt.hqgl_idea.util.ResponseBody;
import com.nimt.hqgl_idea.entity.Goods;
import com.nimt.hqgl_idea.entity.GoodsUseRecord;
import com.nimt.hqgl_idea.mapper.GoodsMapper;
import com.nimt.hqgl_idea.param.GoodsParam;
import com.nimt.hqgl_idea.util.ErrorEnum;
import com.nimt.hqgl_idea.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    GoodsMapper goodsMapper;
    @Override
    public ResponseBody query(GoodsParam goodsParam) {
        ResponseBody responseBody = null;
        goodsParam.setTotalCount(goodsMapper.count(goodsParam.getKeywords()));
        int start = (goodsParam.getCurrentPage() - 1)*goodsParam.getPageSize();
        List<Goods> goods = goodsMapper.selectAll(start, goodsParam.getPageSize(), goodsParam.getKeywords());
        if(goods == null || goods.size() == 0){
            responseBody = new ResponseBody(ErrorEnum.No_More_Data);
        }else {
            goodsParam.setData(goods);
            responseBody = new ResponseBody(goodsParam);
        }
        return responseBody;
    }

    @Transactional
    @Override
    public ResponseBody stock(GoodsVo goodsVo) {
        ResponseBody responseBody = null;
        Goods goodOne = goodsMapper.selectById(goodsVo.getId());
        int stock = goodOne.getStock();
        if(goodsVo.getQuantity()<0 && stock<(0 - goodsVo.getQuantity())){
            responseBody = new ResponseBody(ErrorEnum.Stock_Not_Enough);
        }else {
            Goods goods = new Goods();
            int newStock= (stock + goodsVo.getQuantity());
            goods.setId(goodsVo.getId());
            goods.setStock(newStock);
            int i = goodsMapper.update(goods);
            GoodsUseRecord record = new GoodsUseRecord();
            record.setGoodsId(goodsVo.getId());
            record.setUserId(goodsVo.getUserId());
            record.setQuantity(goodsVo.getQuantity());
            int j = goodsMapper.insertGoodsUseRecord(record);
            if (i > 0 && j > 0) {
                String msg = goodsVo.getQuantity() > 0 ? "补充库存成功!" : "领取成功!";
                responseBody = new ResponseBody(msg);
            }
        }
        return responseBody;
    }

    @Override
    public ResponseBody updateGoods(Goods goods) {
        ResponseBody responseBody = null;
        Goods goodOne = goodsMapper.selectById(goods.getId());
        if (goodOne != null) {
            goodsMapper.update(goods);
            responseBody = new ResponseBody(goods);
        } else {
            responseBody = new ResponseBody("该物资不存在！");
        }
        return responseBody;
    }

    @Override
    public ResponseBody delete(Integer id) {
        ResponseBody responseBody = null;
        if(goodsMapper.delete(id)>0){
            responseBody = new ResponseBody("删除成功!");
        }else {
            responseBody = new ResponseBody(ErrorEnum.Delete_Error);
        }
        return responseBody;
    }

    @Override
    public ResponseBody add(Goods goods) {
        ResponseBody responseBody = null;
        goodsMapper.insert(goods);
        if(goods.getId()!=null){
            responseBody = new ResponseBody(goods);
        }else {
            responseBody = new ResponseBody(ErrorEnum.Insert_Faild);
        }
        return responseBody;
    }
}
